//
//  AppDelegate.m
//  网易新闻
//
//  Created by dllo on 15/9/14.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "AppDelegate.h"
#import "XinwenController.h"
#import "FaxianController.h"
#import "ShitingController.h"
#import "YueduController.h"
#import "WoController.h"
#import "Settings.h"
#import <ShareSDK/ShareSDK.h>
@interface AppDelegate ()<UIScrollViewDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    XinwenController *xinwen = [[XinwenController alloc]init];
    UINavigationController *xinwenNavi = [[UINavigationController alloc]initWithRootViewController:xinwen];
    [xinwen release];
    xinwenNavi.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"新闻" image:[[UIImage imageNamed:@"xinwen.jpg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:100];
    
    FaxianController *faxian = [[FaxianController alloc]init];
    UINavigationController *faxianNavi = [[UINavigationController alloc]initWithRootViewController:faxian];
    [faxian release];
    faxianNavi.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"发现" image:[[UIImage imageNamed:@"faxian.jpg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:101];
    
    ShitingController *shiting = [[ShitingController alloc]init];
    UINavigationController *shitingNavi = [[UINavigationController alloc]initWithRootViewController:shiting];
    [shiting release];
    shitingNavi.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"视听" image:[[UIImage imageNamed:@"shiting.jpg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:102];
    
    YueduController *yuedu = [[YueduController alloc]init];
    UINavigationController *yueduNavi = [[UINavigationController alloc]initWithRootViewController:yuedu];
    [yuedu release];
    yueduNavi.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"阅读" image:[[UIImage imageNamed:@"yuedu.jpg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:103];
    
    WoController *wo = [[WoController alloc]init];
    UINavigationController *woNavi = [[UINavigationController alloc]initWithRootViewController:wo];
    [wo release];
    woNavi.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我" image:[[UIImage imageNamed:@"wo.jpg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:104];

    
    UITabBarController *main = [[UITabBarController alloc]init];
    main.viewControllers = [NSArray arrayWithObjects:xinwenNavi, yueduNavi, shitingNavi, faxianNavi, woNavi, nil];
    main.selectedIndex = 0;
    [xinwenNavi release];
    [faxianNavi release];
    [shitingNavi release];
    [yueduNavi release];
    [woNavi release];
    self.window.rootViewController = main;
    
    [self createGuide];
    [self createShareSDK];
    [main release];
    [_window release];
    return YES;
}

- (void)createGuide{
    //引导页
    if ([[Settings shareSettings]isFirst]) {
        UIScrollView *guide = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        guide.contentSize = CGSizeMake(375*5, 667);
        guide.backgroundColor = [UIColor clearColor];
        guide.pagingEnabled = YES;
        guide.bounces = NO;
        guide.showsHorizontalScrollIndicator = YES;
        guide.showsVerticalScrollIndicator = NO;
        guide.delegate = self;
        for (int i=0; i<4; i++){
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(i*375, 0, 375, 667)];
            img.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@/%d.jpg",[[NSBundle mainBundle]bundlePath], i+1]];
            [guide addSubview:img];
            [img release];
        }
        [self.window addSubview:guide];
        [self.window bringSubviewToFront:guide];
        [guide release];
        [[Settings shareSettings]changeFirst];
    }
}

- (void)createShareSDK{
    [ShareSDK registerApp:@"iosv1101" activePlatforms:@[@(SSDKPlatformTypeSinaWeibo),@(SSDKPlatformTypeTencentWeibo),@(SSDKPlatformTypeFacebook),@(SSDKPlatformTypeTwitter)] onImport:nil onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
        switch (platformType) {
            case SSDKPlatformTypeSinaWeibo:
                [appInfo SSDKSetupSinaWeiboByAppKey:@"568898243" appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3" redirectUri:@"http://www.sharesdk.cn" authType:SSDKAuthTypeBoth];
                break;                
            default:
                break;
        }
    }];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x > 375*3) {
        scrollView.hidden = YES;
    }
}

- (void)dealloc{
    [_window release];
    [super dealloc];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
